<?php

namespace App\Http\Controllers;

use DB;
use App\Quotation;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Config;


class AttentionController extends Controller {

    public $data = array();

    public $goAuth = false;
    public $goReload = false;

    public function __construct(){
        session_start();
        $this->data['user_agent'] = $this->getUserAgent();

        if( empty($_SESSION['user_id']) ){
            $this->goAuth = true;
        }
    }


    function getAllAttentions(){
        if( $this->goAuth ){
            $state          = urlencode("http://".env("APP_URL")."/attention");
            $redirectUrl    = Config::get("lib.oauth_uri")."/auth?client_id=".Config::get("lib.client_id")."&response_type=code&state=$state";
            return redirect($redirectUrl);
        }

        $user_id = $_SESSION['user_id'];

        //查找所有关注的问题
        $questions = DB::table('question')
                        ->select('question.id','question.title')
                        ->join('user_question','question.id','=','user_question.question_id')
                        ->where('user_question.user_id','=',$user_id)
                        ->where('question.is_deleted','=',0)
                        ->where('user_question.is_deleted','=',0)
                        ->orderBy('user_question.add_time' , 'DESC')
                        ->get();

        if( count($questions) > 0 ){
            $question_id_arr = array();
            foreach( $questions as $key => $val ){
                $question_id_arr[] = $val->id;
            }
            $str = implode(',' , $question_id_arr);
            //查找回答
            $answers = DB::select("select
                                    question_id , count(question_id) as temp_count
                                    from
                                    answer
                                    where
                                    question_id in ($str)
                                    group by
                                    question_id
                                    ");
            //查找关注
            $attentions = DB::select("select
                                        question_id , count(question_id) as temp_count
                                        from
                                        user_question
                                        where
                                        question_id in ($str)
                                        group by
                                        question_id
                                        ");
            $id_to_answers = array();
            $id_to_attentions = array();
            foreach( $answers as $key => $val ){
                $id_to_answers[$val->question_id] = $val->temp_count;
            }
            foreach( $attentions as $key => $val ){
                $id_to_attentions[$val->question_id] = $val->temp_count;
            }
            $this->data['answers_count'] = $id_to_answers;
            $this->data['attentions_count'] = $id_to_attentions;
        }

        $this->data['questions'] = $questions;
        return view('attentions' , $this->data);
    }


    function doAttention($id){
        if( !Input::has('tag') ){
            return Response::json(['error'=>true , 'message'=>'无法操作'] , 200);
        }
        $tag = Input::get('tag');
        $user_id = $_SESSION['user_id'];

        if( $tag == 1 ){
            DB::table('user_question')->where('question_id','=',$id)->where('user_id','=',$user_id)->delete();
            return Response::json(['error'=>false , 'message'=>'操作成功'] , 200);
        }else{
            $insert_arr = array(
                'question_id' => $id ,
                'user_id' => $user_id ,
                'add_time' => date('Y-m-d H:i:s' , time())
                );
            $insert_id = DB::table('user_question')->insertGetId($insert_arr);
            if( $insert_id > 0 ){
                return Response::json(['error'=>false , 'message'=>'操作成功'] , 200);
            }else{
                return Response::json(['error'=>true , 'message'=>'操作失败'] , 200);
            }
        }
    }


}
